<template>
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      />
      <page-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        ref="depotTable"
        v-model:data="tableInfo.data"
        :api="api"
        :listen-height="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="highlightRow"
        :check-box="checkBox"
        :isDynamic="true"
        @handleEvent="handleEvent"
      >
      </page-table>
    </page-dialog>
  </div>
</template>

<script setup name="selectInventoryOrder">
import PageDialog from '@/components/PageDialog/index'
import PageTable from '@/components/PageTable/index'
import PageFilter from '@/components/PageFilter/index'
import { getInventoryOrderPage } from '@/api/stock.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getInventoryOrderPage,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  defaultList: {
    type: Array,
    default: function () {
      return []
    },
  },
})

// 过滤相关配置
const filterInfo = reactive({
  query: {
    statusIn: '2,4',
    sort: 'create_time desc',
    inventoryType: 2,
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '单号',
      value: 'orderNoLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '单号', value: 'orderNo' },
    { label: '仓库', value: 'depotName' },
    { label: '盘点人', value: 'inventoryUserName' },
    { label: '盘点时间', value: 'inventoryTime' },
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '选择盘点单',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: false,
      event: save,
      show: true,
    },
  ],
})

const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})

watch(
  () => props.showDialog,
  (val) => {
    if (val) {
      proxy.$nextTick(() => {
        getList()
      })
    }
  }
)

onMounted(() => {
  proxy.$nextTick(() => {
    getList()
    if (props.defaultList.length) {
      selectRow.value = props.defaultList
      props.defaultList.forEach((item) => {
        proxy.$refs['depotTable'].toggleRowSelection(item)
      })
    }
  })
})

// 获取列表
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function save() {
  if (selectRow.value) {
    handleClose()
    emit('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择单据',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      // dialogInfo.btList[1].disabled = !selectRow.value;
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        // dialogInfo.btList[1].disabled = !selectRow.value;
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emit('reset')
}
</script>

<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
